package cn.gov.msa.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.msa.dao.MaritimeOrgMapper;
import cn.gov.msa.model.Org;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.service.OrgService;
import cn.gov.msa.util.InfoObjUtil;
import cn.gov.msa.util.WebServiceTools;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/org", produces="text/plain;charset=UTF-8")
public class OrgController {
	
	ObjectMapper mapper = new ObjectMapper();
	@Autowired
	MaritimeOrgMapper maritimeOrgMapper;
	@Autowired
	private OrgService orgService;
	
	@RequestMapping(value = "/getOrg")
	public String getDictDetail(String orgLevel,String orgType) throws Exception{
		Org org=new Org();
		org.setOrgLevel(Integer.parseInt(orgLevel));
		org.setOrgType(Integer.parseInt(orgType));
		String result = "";
		String info = "";
		JSONObject jsonResult = new JSONObject();
		
		try{
			List<Org> OrgList =orgService.selectOrg(org);
	
			JSONArray japaram = JSONArray.fromObject(OrgList);
			
			result = OrgList.size() > 0 ? "1" : "0";
			info = OrgList.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
	
			jsonResult.put("datas", japaram);	
		}
		catch(Exception e){
			result = "2";
			info = InfoObjUtil.Exption_msg;
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);	
		
		System.err.println(jsonResult);
		
		return jsonResult.toString();
	}

	@RequestMapping(value = "/getOrg1")
	public String getOrg(String orglevel, String orgtype, HttpSession session) throws Exception {

//		String data = "{\"hot\":[],\"province\":[{\"id\":\"010000\",\"name\":\"上海海事局\",\"city\":[{\"id\":\"010000\",\"name\":\"上海海事局\"},{\"id\":\"010100\",\"name\":\"吴淞海事局\"},{\"id\":\"010200\",\"name\":\"闵行海事局\"},{\"id\":\"010300\",\"name\":\"黄浦海事局\"},{\"id\":\"010400\",\"name\":\"杨浦海事局\"},{\"id\":\"010500\",\"name\":\"金山海事局\"},{\"id\":\"010600\",\"name\":\"崇明海事局\"},{\"id\":\"010700\",\"name\":\"浦东海事局\"},{\"id\":\"010800\",\"name\":\"宝山海事局\"},{\"id\":\"011000\",\"name\":\"洋山港海事局\"}]},{\"id\":\"020000\",\"name\":\"天津海事局\",\"city\":[{\"id\":\"020000\",\"name\":\"天津海事局\"},{\"id\":\"020100\",\"name\":\"新港海事局\"},{\"id\":\"020200\",\"name\":\"南疆海事局\"},{\"id\":\"020300\",\"name\":\"海河海事局\"},{\"id\":\"020500\",\"name\":\"北疆海事局\"},{\"id\":\"020800\",\"name\":\"东疆海事局\"},{\"id\":\"020900\",\"name\":\"大沽口海事局\"}]},{\"id\":\"030000\",\"name\":\"辽宁海事局\",\"city\":[{\"id\":\"030000\",\"name\":\"辽宁海事局\"},{\"id\":\"030100\",\"name\":\"大连海事局\"},{\"id\":\"030200\",\"name\":\"丹东海事局\"},{\"id\":\"030300\",\"name\":\"锦州海事局\"},{\"id\":\"030400\",\"name\":\"葫芦岛海事局\"},{\"id\":\"150000\",\"name\":\"营口海事局\"}]},{\"id\":\"040000\",\"name\":\"河北海事局\",\"city\":[{\"id\":\"040000\",\"name\":\"河北海事局\"},{\"id\":\"040100\",\"name\":\"秦皇岛海事局\"},{\"id\":\"040200\",\"name\":\"沧州海事局\"},{\"id\":\"040300\",\"name\":\"唐山海事局\"},{\"id\":\"040302\",\"name\":\"曹妃甸海事处\"}]},{\"id\":\"050000\",\"name\":\"山东海事局\",\"city\":[{\"id\":\"050000\",\"name\":\"山东海事局\"},{\"id\":\"050100\",\"name\":\"青岛海事局\"},{\"id\":\"050200\",\"name\":\"济南海事局\"},{\"id\":\"050300\",\"name\":\"威海海事局\"},{\"id\":\"050400\",\"name\":\"日照海事局\"},{\"id\":\"160000\",\"name\":\"烟台海事局\"}]},{\"id\":\"060000\",\"name\":\"江苏海事局\",\"city\":[{\"id\":\"060000\",\"name\":\"江苏海事局\"},{\"id\":\"060100\",\"name\":\"南京海事局\"},{\"id\":\"060200\",\"name\":\"张家港海事局\"},{\"id\":\"060300\",\"name\":\"南通海事局\"},{\"id\":\"060400\",\"name\":\"镇江海事局\"},{\"id\":\"060600\",\"name\":\"江阴海事局\"},{\"id\":\"060700\",\"name\":\"扬州海事局\"},{\"id\":\"060800\",\"name\":\"常州海事局\"},{\"id\":\"060900\",\"name\":\"泰州海事局\"},{\"id\":\"061000\",\"name\":\"常熟海事局\"},{\"id\":\"061100\",\"name\":\"太仓海事局\"},{\"id\":\"061200\",\"name\":\"宜兴海事处\"},{\"id\":\"061300\",\"name\":\"如皋海事局\"},{\"id\":\"061400\",\"name\":\"盐城海事局\"},{\"id\":\"170000\",\"name\":\"连云港海事局\"}]},{\"id\":\"070000\",\"name\":\"浙江海事局\",\"city\":[{\"id\":\"070000\",\"name\":\"浙江海事局\"},{\"id\":\"070100\",\"name\":\"宁波海事局\"},{\"id\":\"070200\",\"name\":\"嘉兴海事局\"},{\"id\":\"070300\",\"name\":\"舟山海事局\"},{\"id\":\"070400\",\"name\":\"温州海事局\"},{\"id\":\"070500\",\"name\":\"台州海事局\"},{\"id\":\"070600\",\"name\":\"杭州海事局\"}]},{\"id\":\"080000\",\"name\":\"福建海事局\",\"city\":[{\"id\":\"080000\",\"name\":\"福建海事局\"},{\"id\":\"080100\",\"name\":\"福州海事局\"},{\"id\":\"080200\",\"name\":\"宁德海事局\"},{\"id\":\"080300\",\"name\":\"莆田海事局\"},{\"id\":\"080400\",\"name\":\"泉州海事局\"},{\"id\":\"080600\",\"name\":\"平潭海事处\"},{\"id\":\"180000\",\"name\":\"厦门海事局\"},{\"id\":\"180100\",\"name\":\"漳州海事局\"}]},{\"id\":\"090000\",\"name\":\"广东海事局\",\"city\":[{\"id\":\"090000\",\"name\":\"广东海事局\"},{\"id\":\"090100\",\"name\":\"广州海事局\"},{\"id\":\"090300\",\"name\":\"珠海海事局\"},{\"id\":\"090400\",\"name\":\"惠州海事局\"},{\"id\":\"090500\",\"name\":\"河源海事局\"},{\"id\":\"090600\",\"name\":\"汕尾海事局\"},{\"id\":\"090700\",\"name\":\"江门海事局\"},{\"id\":\"090800\",\"name\":\"阳江海事局\"},{\"id\":\"090900\",\"name\":\"中山海事局\"},{\"id\":\"091000\",\"name\":\"佛山海事局\"},{\"id\":\"091100\",\"name\":\"肇庆海事局\"},{\"id\":\"091200\",\"name\":\"云浮海事局\"},{\"id\":\"091300\",\"name\":\"清远海事局\"},{\"id\":\"091400\",\"name\":\"韶关海事局\"},{\"id\":\"091500\",\"name\":\"梅州海事局\"},{\"id\":\"091600\",\"name\":\"茂名海事局\"},{\"id\":\"091700\",\"name\":\"潮州海事局\"},{\"id\":\"091800\",\"name\":\"揭阳海事局\"},{\"id\":\"190000\",\"name\":\"汕头海事局\"},{\"id\":\"200000\",\"name\":\"湛江海事局\"}]},{\"id\":\"100000\",\"name\":\"广西海事局\",\"city\":[{\"id\":\"100000\",\"name\":\"广西海事局\"},{\"id\":\"100100\",\"name\":\"南宁海事局\"},{\"id\":\"100200\",\"name\":\"北海海事局\"},{\"id\":\"100300\",\"name\":\"防城港海事局\"},{\"id\":\"100400\",\"name\":\"钦州海事局\"},{\"id\":\"100500\",\"name\":\"柳州海事局\"},{\"id\":\"100600\",\"name\":\"河池海事局\"},{\"id\":\"100700\",\"name\":\"桂林海事局\"},{\"id\":\"100800\",\"name\":\"贵港海事局\"},{\"id\":\"100900\",\"name\":\"梧州海事局\"},{\"id\":\"101000\",\"name\":\"百色海事局\"}]},{\"id\":\"110000\",\"name\":\"海南海事局\",\"city\":[{\"id\":\"110000\",\"name\":\"海南海事局\"},{\"id\":\"110100\",\"name\":\"海口海事局\"},{\"id\":\"110200\",\"name\":\"清澜海事局\"},{\"id\":\"110300\",\"name\":\"三亚海事局\"},{\"id\":\"110400\",\"name\":\"八所海事局\"},{\"id\":\"110500\",\"name\":\"洋浦海事局\"},{\"id\":\"110600\",\"name\":\"三沙海事局\"}]},{\"id\":\"120000\",\"name\":\"长江海事局\",\"city\":[{\"id\":\"120000\",\"name\":\"长江海事局\"},{\"id\":\"120100\",\"name\":\"重庆海事局\"},{\"id\":\"120300\",\"name\":\"重庆万州海事处\"},{\"id\":\"120400\",\"name\":\"重庆涪陵海事处\"},{\"id\":\"120500\",\"name\":\"宜昌海事局\"},{\"id\":\"120600\",\"name\":\"荆州海事局\"},{\"id\":\"120700\",\"name\":\"岳阳海事局\"},{\"id\":\"120800\",\"name\":\"武汉海事局\"},{\"id\":\"120900\",\"name\":\"黄石海事局\"},{\"id\":\"121000\",\"name\":\"九江海事局\"},{\"id\":\"121100\",\"name\":\"安庆海事局\"},{\"id\":\"121200\",\"name\":\"芜湖海事局\"},{\"id\":\"121500\",\"name\":\"三峡海事局\"}]},{\"id\":\"130000\",\"name\":\"黑龙江海事局\",\"city\":[{\"id\":\"130000\",\"name\":\"黑龙江海事局\"},{\"id\":\"130100\",\"name\":\"哈尔滨海事局\"},{\"id\":\"130200\",\"name\":\"佳木斯海事局\"},{\"id\":\"130300\",\"name\":\"黑河海事局\"},{\"id\":\"130400\",\"name\":\"齐齐哈尔海事局\"},{\"id\":\"130500\",\"name\":\"牡丹江海事局\"}]},{\"id\":\"140000\",\"name\":\"深圳海事局\",\"city\":[{\"id\":\"140000\",\"name\":\"深圳海事局\"},{\"id\":\"140100\",\"name\":\"大亚湾海事局\"},{\"id\":\"140200\",\"name\":\"蛇口海事局\"},{\"id\":\"140300\",\"name\":\"盐田海事局\"},{\"id\":\"140400\",\"name\":\"宝安海事局\"},{\"id\":\"140500\",\"name\":\"南山海事局\"},{\"id\":\"140600\",\"name\":\"大铲海事局\"}]}]}";
//		JSONObject jsonResultData = JSONObject.fromObject(data);
		
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		JSONObject jsonResultData = orgService.getOrg(userDetail.getOrgcode());
		if(jsonResultData.isNullObject() || jsonResultData.isEmpty()){
			return "{\"hot\":[],\"province\":[]}";

		}
		return jsonResultData.toString();
	}
	
	/**
	 * 新机构控件数据请求地址
	 * @param orglevel
	 * @param orgtype
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getOrg2")
	public String getOrg2(HttpSession session) throws Exception {

		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		
		Org org = new Org();
		org.setOrgCode(userDetail.getOrgcode());
		
		List<Map<String, Object>> orgList = orgService.selectAllOrg(org);
		
		JSONObject jsonOrgArray = new JSONObject();
		
		jsonOrgArray.put("orglist", JSONArray.fromObject(orgList));
		jsonOrgArray.put("currentOrgcode", userDetail.getOrgcode());
		
		return jsonOrgArray.toString();
	}
	
	@RequestMapping(value = "/initOrg")
	public void initOrg(String orglevel, String orgtype) throws Exception {
		WebServiceTools.dayin("00", (short) 1,maritimeOrgMapper);
	}
	
	@RequestMapping(value = "/getOrgControl")
	public String getDictDetail(@RequestBody JSONObject jsonParams,HttpSession session) throws Exception{
		UserDetail userDetail=(UserDetail) session.getAttribute("userDetail");
	
		Org org = new Org();
		if(jsonParams.has("orglevel") && jsonParams.getString("orglevel").length() != 0)
			org.setOrgLevel(jsonParams.getInt("orglevel"));
		if(jsonParams.has("orgtype") && jsonParams.getString("orgtype").length() != 0)
			org.setOrgType(jsonParams.getInt("orgtype"));
		if(jsonParams.has("porgcode") && jsonParams.getString("porgcode").length() != 0)
			org.setPorgCode(jsonParams.getString("porgcode"));      
        JSONArray jaResult = JSONArray.fromObject(orgService.selectOrg(org));
        JSONObject json = new JSONObject();
        if (jaResult.size() > 0) {
        	JSONArray orgList = new JSONArray();
        	for(int i = 0; i < jaResult.size(); i++){
        		JSONObject porg = new JSONObject();
        		porg.put("id", jaResult.getJSONObject(i).getString("orgCode4A"));
        		porg.put("name", jaResult.getJSONObject(i).getString("orgName"));
        		// 搜索下级机构
        		org.setOrgCode(jaResult.getJSONObject(i).getString("orgCode"));
        		JSONArray childOrgResult = JSONArray.fromObject(orgService.queryNextOrg(org));
        		if(childOrgResult.size() > 0){
        			JSONObject childOrg = new JSONObject();
        			JSONArray childOrgList = new JSONArray();
        			for (int j = 0; j < childOrgResult.size(); j++) {
        				childOrg.put("id", childOrgResult.getJSONObject(j).getString("orgCode4A"));
        				childOrg.put("name", childOrgResult.getJSONObject(j).getString("orgName"));
        				childOrgList.add(childOrg);
        			}
        			porg.put("city", childOrgList);
        		}
        		orgList.add(porg);
        	}
        	json.put("province", orgList);
        }
		
		 //获取当前机构作为常用机构
        Org orgParam=new Org();
        orgParam.setOrgCode(userDetail.getOrgcode());
        JSONArray hotOrgResult = JSONArray.fromObject(orgService.selectOrg(orgParam));
		if(hotOrgResult.size() > 0){
			JSONArray hotOrgList = new JSONArray();
			for (int i = 0; i < hotOrgResult.size(); i++) {
				JSONObject hotOrg = new JSONObject();
				hotOrg.put("id", hotOrgResult.getJSONObject(i).getString("orgCode4A"));
				hotOrg.put("name", hotOrgResult.getJSONObject(i).getString("orgName"));
				hotOrgList.add(hotOrg);
			}
			json.put("hot", hotOrgList);
		}
		return json.toString();
	}
	
	@RequestMapping(value = "/getPOrg")
	public String getPOrg(@RequestBody JSONObject jsonParams, HttpSession session) throws Exception{
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
	    JSONObject jsonResult = new JSONObject();
		
		try{
			Org org = new Org();
			if(jsonParams.has("orglevel") && jsonParams.getString("orglevel").length() != 0)
				org.setOrgLevel(jsonParams.getInt("orglevel"));
			if(jsonParams.has("orgtype") && jsonParams.getString("orgtype").length() != 0)
				org.setOrgType(jsonParams.getInt("orgtype"));
			if(jsonParams.has("porgcode") && jsonParams.getString("porgcode").length() != 0)
				org.setPorgCode(jsonParams.getString("porgcode"));
	        
	        JSONArray jaResult = JSONArray.fromObject(orgService.selectOrg(org));

	        if(jaResult.size() > 0){
	        	result = 1;
	        	info = InfoObjUtil.Success_select_OK;
	        	jsonResult.put("datas", jaResult);
	        }
		}
		catch(Exception e){
			e.printStackTrace();
        	result = 2;
        	info = InfoObjUtil.Exption_msg;
		}
		

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);

		
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/getChildOrg")
	public String getChildOrg(@RequestBody JSONObject jsonParams, HttpSession session) throws Exception{
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
	    JSONObject jsonResult = new JSONObject();
		
		try{
			Org org = new Org();
			org.setOrgCode(jsonParams.getString("orgCode"));
	        
	        JSONArray jaResult = JSONArray.fromObject(orgService.queryNextOrg(org));
	        if(jaResult.size() > 0){
	        	result = 1;
	        	info = InfoObjUtil.Success_select_OK;
	        	jsonResult.put("datas", jaResult);
	        }
		}
		catch(Exception e){
			e.printStackTrace();
        	result = 2;
        	info = InfoObjUtil.Exption_msg;
		}
		

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
		
		return jsonResult.toString();
	}
}
